package com.like.likeutils.util;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ListViewUtil {
	
	public static void setListViewHeightBasedOnChildren(ListView listView, boolean focusable) {  
        ListAdapter listAdapter = listView.getAdapter();   
        if (listAdapter == null) {  
            return;  
        }  
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {  
            View listItem = listAdapter.getView(i, null, listView);  
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height =  totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        params.height = height;
        listView.setLayoutParams(params);
        if(!focusable) {
	        listView.setFocusable(false);
	        listView.setClickable(false);
	        listView.setEnabled(false);
        }
    }
	
}
